package com.parse.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.parse.api.WebComponet;
import com.parse.contract.Args;

public class WebComponetImpl implements WebComponet {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.WebComponet#getPostData(java.lang.String,
	 * java.util.List)
	 */
	@Override
	public String getPostData(String url, List<NameValuePair> parms)
			throws Exception {
		String out = "";
		HttpClient client = new DefaultHttpClient();
		try {

			HttpPost post = new HttpPost(url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.addAll(parms);

			UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,
					HTTP.UTF_8);
			post.setEntity(ent);
			HttpResponse responsePOST = client.execute(post);
			if (responsePOST.getStatusLine().getStatusCode() == 200) {
				HttpEntity resEntity = responsePOST.getEntity();
				out = EntityUtils.toString(resEntity);
			} else {
				out = "";
			}

		} catch (Exception e) {

			out = "";
		} finally {
			client.getConnectionManager().shutdown();
		}
		return out;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.WebComponet#getGetData(java.lang.String)
	 */
	@Override
	public String getGetData(String url) throws Exception {
		String out = "";
		HttpClient client = new DefaultHttpClient();
		try {

			HttpGet get = new HttpGet(url);
			HttpResponse response = client.execute(get);
			HttpEntity resEntity = response.getEntity();

			if (response.getStatusLine().getStatusCode() == 200) {
				out = EntityUtils.toString(resEntity);
			} else {
				out = "";
			}

		} catch (Exception e) {
			e.printStackTrace();
			out = "";
			// throw e;
		} finally {
			client.getConnectionManager().shutdown();
		}
		return out;
	}

	@Override
	public Bitmap getBitmapScale(String url, float scaleXY) throws Exception {

		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setReadTimeout(30000);
			conn.setDoInput(true);

			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);

			Matrix matrix = new Matrix();
			matrix.postScale(scaleXY, scaleXY); // 长和宽放大缩小的比例
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);

			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	public Bitmap getBitmap(String url) throws Exception {
		return this.getBitmapScale(url, 1f);
	}

	// @Override
	// public String post(String urlPath, String parms) throws Exception {
	//
	// String urlParameters = "user=caterpillar&passwd=123456";
	// String request = "http://myappmap1987.appspot.com/login.html";
	// URL url = new URL(request);
	// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	// connection.setDoOutput(true);
	// connection.setDoInput(true);
	// connection.setInstanceFollowRedirects(false);
	// connection.setRequestMethod("POST");
	// // connection.setRequestProperty("Content-Type",
	// // "application/x-www-form-urlencoded");
	// connection.setRequestProperty("charset", "utf-8");
	// connection.setRequestProperty("Content-Length",
	// "" + Integer.toString(urlParameters.getBytes().length));
	// connection.setUseCaches(false);
	//
	// DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
	// wr.writeBytes(urlParameters);
	// wr.flush();
	// wr.close();
	// connection.disconnect();
	// return "";
	//
	// }
}
